package com.broada.apm.ajax;

import java.util.List;

import com.broada.apm.entity.ApplicationSystem;
import com.broada.apm.entity.DomainPerformance;
import com.broada.apm.entity.ErrorCodeCount;
import com.broada.apm.entity.PerformanceGroup;
import com.broada.apm.entity.Threshold;
import com.broada.apm.entity.TradePerformance;
import com.broada.apm.entity.Transaction;
import com.broada.apm.model.PagingList;
import com.broada.apm.model.PerformanceView;
import com.broada.apm.util.Constants;
import com.google.common.collect.Lists;

public class PerformanceResult extends Result {

	/**
	 * @Fields serialVersionUID :
	 */

	private static final long serialVersionUID = 1L;

	public void setThreshold(Threshold threshold) {
		this.put("threshold", threshold);
	}

	public void setPerformanceGroup(List<PerformanceGroup> groups) {
		if (groups != null) {
			for (PerformanceGroup performanceGroup : groups) {
				this.put(performanceGroup.getId(), performanceGroup.getPerformances());
				this.put(performanceGroup.getId() + "lastPerf", performanceGroup.getLastPerf());
			}
		}
	}

	public void setPerformances(List<? extends PerformanceView> list) {
		this.put(Constants.PERFORMANCES, list);
	}

	public void setPerformancesResult(List<? extends PerformanceView> list) {
		this.put("result", list);
	}

	public void setMapType(String mapType) {
		this.put("mapType", mapType);
	}

	public void setRequeset(long request) {
		this.put("request", request);
	}

	public void setResponseTime(double responseTime) {
		this.put("responseTime", responseTime);
	}

	public void setConcurrentUsers(long concurrentUsers) {
		this.put("concurrentUsers", concurrentUsers);
	}

	public void setApdex(int apdex) {
		this.put("apdex", apdex);
	}

	public void setError(long error) {
		this.put("error", error);
	}

	public void setErrorCode(List<ErrorCodeCount> errorCode) {
		this.put("errorCode", errorCode);
	}

	public void setPageList(PagingList<PerformanceView> perfs) {
		this.put("result", perfs);
	}

	public void setDomainPerformances(List<DomainPerformance> domainPerfs) {
		this.put("result", domainPerfs);
	}
	
	public void setTradePerformance(List<TradePerformance> tradePerformance) {
		this.put("result", tradePerformance);
	}
	
	public void setPerformanceGroupResult(List<PerformanceGroup> performanceGroup) {
		this.put("result", performanceGroup);
	}
	
	public void setApplications(List<ApplicationSystem> list) {
		this.put(Constants.APPLICATIONS, list);
	}
	
	public void setTransactionMerge(List<Transaction> list) {
		this.put("result", list);
	}

	/**
	 * 内存分页处理
	 * 
	 * @param pageSize
	 * @param pageIndex
	 * @param list
	 */
	public PerformanceResult pagingHandle(int pageSize, int pageIndex, List<?> list) {

		if (list == null || list.size() == 0) {
			this.put("result", Lists.newArrayList());
			return this;
		}

		int totalPage = 0;
		int count = list.size();
		int number = count % pageSize;
		this.setPageSize(pageSize);
		this.setTotal(count);
		this.setPageIndex(pageIndex);
		if (number > 0) {
			totalPage = count / pageSize + 1;
		} else {
			totalPage = count / pageSize;
		}
		this.setTotalPage(totalPage);
		if (pageIndex < totalPage - 1) {
			list = list.subList(pageIndex * pageSize, (pageIndex + 1) * pageSize);
		} else {
			list = list.subList((totalPage - 1) * pageSize, list.size());
		}
		this.put("result", list);
		return this;
	}
}
